var tools = {};
/*
 设置日期格式化函数 Date now = new Date();var dateStr = now.format("yyyy-MM-dd hh:mm:ss");
 */
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	};
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
};
/*
 将json对象转换为字符串 var json = {id=3,name="abc"};var str = tools.j2s(json);
 */
tools.j2s = function(O) {
	var S = [];
	var J = "";
	if (Object.prototype.toString.apply(O) === '[object Array]') {
		for ( var i = 0; i < O.length; i++) {
			S.push(this.j2s(O[i]));
		}
		J = '[' + S.join(',') + ']';
	} else if (Object.prototype.toString.apply(O) === '[object Date]') {
		J = "new Date(" + O.getTime() + ")";
	} else if (Object.prototype.toString.apply(O) === '[object RegExp]'
			|| Object.prototype.toString.apply(O) === '[object Function]') {
		J = O.toString();
	} else if (Object.prototype.toString.apply(O) === '[object Object]') {
		for ( var i in O) {
			var tempObj = "";
			if (typeof (O[i]) == 'string') {
				tempObj = '"' + O[i] + '"';
			} else if (typeof (O[i]) === 'object') {
				tempObj = this.j2s(O[i]);
			} else {
				tempObj = O[i];
			}
			S.push(i + ':' + tempObj);
		}
		J = '{' + S.join(',') + '}';
	} else {
		J = O;
	}
	return J;
};
//json字符串转换为json对象
tools.s2j = function(jsonString) {
	return eval('(' + jsonString + ')');
};
//JSON字符串到Object
tools.json2Obj = function(_json) {
	if (_json == '') {
		//alert("函数初始化失败！");
		return "";
	}
	return eval("({root:" + _json + "})");
};
//制保留2位小数，如：2，会在2后面补上00.即2.00          
tools.toDecimal = function(x) {
	var f = parseFloat(x);
	if (isNaN(f)) {
		return false;
	}
	var f = Math.round(x * 100) / 100;
	var s = f.toString();
	var rs = s.indexOf('.');
	if (rs < 0) {
		rs = s.length;
		s += '.';
	}
	while (s.length <= rs + 2) {
		s += '0';
	}
	return s;
};